package cn.lliiooll.mt;

import cn.lliiooll.Mentor;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Bed;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class Confirm {

    private static Map<Object,Object[]> m = new HashMap<Object,Object[]>();

    /**
     *  Object[] o;
     *  o[0] 为操作
     *  0[1] 为时间
     *  0[2] 为发送请求的玩家
     *  在拜师收徒时，key将为被发送请求的人
     *
     */


    public static void add(Object key,Object[] value){
        if(m.get(key) != null){
            Mentor.m .info("你已经有一个请求了!",Bukkit.getPlayer((UUID) new Confirm().get(key)[2]));
            return;
        }
        m.put(key,value);
        new Thread(new ConfirmThread((Integer) value[1],key), key.toString()).start();
    }

    public static Object[] get(Object key){
        return m.get(key);
    }

    public static void del(Object key){
        m.remove(key);
    }
}

class ConfirmThread implements Runnable{

    private int time = 0;
    private Object key = new Object();

    public ConfirmThread(int time,Object key){
        this.time = time;
        this.key = key;
    }

    @Override
    public void run() {
        int i = this.time;
        Object key = this.key;
        while(true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            i--;
            if(i<0){
                Mentor.m .info("请求已超时",Bukkit.getPlayer((UUID) key));
                Mentor.m .info("请求已超时",(Player)Confirm.get(key)[2]);
                break;
            }
            if(Confirm.get(key) == null){
                break;
            }
        }
        Confirm.del(key);
    }
}
